package cn.he4966.bingguo;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@EnableScheduling
@Component
public class TimerTask {

    // 每5分钟的第0秒执行，然后在执行10秒后继续
    @Scheduled(cron = "0/20 * * * * *")
    public void executeTask() {
        //    @Scheduled(cron = "10 0/1 * * * *")
        System.out.println("任务开始执行：" + LocalDateTime.now());
        String html = "";
        try {
            // 延迟10秒
//            Thread.sleep(10 * 1000);
//            打印出今天的年-月-日 示例2024-08-01
            // 获取当前日期
            LocalDate today = LocalDate.now();

            // 定义日期格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

            // 格式化日期
            String formattedDate = today.format(formatter);

//            System.out.println("当前日期：" + formattedDate);

//            String date = LocalDateTime.now().getYear() + "-" + LocalDateTime.now().getMonthValue() + "-" + LocalDateTime.now().getDayOfMonth();
            String url = "http://127.0.0.1:8080/bingo_get?pageSize=2&pageNum=1&date=" + formattedDate;
            System.out.println(url);

            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url(url)
                    .build();
            Response response = client.newCall(request).execute();
            if (response.isSuccessful() && response.body() != null) {
                html = response.body().string();
//                    System.out.println(html);

                ObjectMapper objectMapper = new ObjectMapper();
                try {
                    JsonNode rootNode = objectMapper.readTree(html);
                    JsonNode dataNode = rootNode.path("data");
                    JsonNode bingoQueryResultNode = dataNode.path("bingoQueryResult");

                    // 假设JSON中只有一个bingoQueryResult对象
                    if (bingoQueryResultNode.isArray()) {
                        JsonNode firstBingoQueryResult = bingoQueryResultNode.get(0);
                        long drawTerm = firstBingoQueryResult.path("drawTerm").asLong();
                        System.out.println("drawTerm: " + drawTerm);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

//            System.out.println(html);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //分割线
        System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
//        System.out.println("任务执行完毕：" + LocalDateTime.now());
    }
}
